package com.hy.service.impl;

import com.hy.mapping.UserMapper;
import com.hy.model.User;
import com.hy.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;

@Service
public class UserServiceImpl implements IUserService {

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

   
    @Transactional(readOnly = true)
    public User login(String loginname, String password) {
        User user = new User();
        user.setLoginname(loginname);
        user.setPassword(password);
        User loginUser = userMapper.selectOne(user);
        return loginUser;
    }

    public User add(User user) {
        if(!checkSameLoginname(user.getLoginname())){
            return user;
        }else {
            userMapper.insertSelective(user);
            return user;
        }
    }

    public boolean checkSameLoginname(String loginname) {
        Example userExample = new Example(User.class);
        userExample.createCriteria().andEqualTo("loginname", loginname);
        return userMapper.selectCountByExample(userExample) == 0;
    }
}
